from typing import Optional


class PracticeProgress:
    """练习进度实体类"""
    def __init__(self,
                 id: Optional[int] = None,
                 category: Optional[str] = None,
                 current_index: Optional[int] = None,
                 last_accessed: Optional[str] = None,
                 user_id: Optional[int] = None):
        """
        初始化练习进度实体
        :param id: 进度ID
        :param category: 题目分类
        :param current_index: 当前索引
        :param last_accessed: 最后访问时间
        :param user_id: 用户ID
        """
        self.id = id
        self.category = category
        self.current_index = current_index
        self.last_accessed = last_accessed
        self.user_id = user_id

    def __dict__(self) -> dict:
        """将实体转换为字典"""
        return {
            'id': self.id,
            'category': self.category,
            'current_index': self.current_index,
            'last_accessed': self.last_accessed,
            'user_id': self.user_id
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'PracticeProgress':
        """从字典创建实体"""
        return cls(
            id=data.get('id'),
            category=data.get('category'),
            current_index=data.get('current_index'),
            last_accessed=data.get('last_accessed'),
            user_id=data.get('user_id')
        )